﻿
Imports std

Namespace storageJobConsumerInternal
    Public Class sync_to_async_wrapper
        Implements iasyncslice

        Private ReadOnly s As isyncslice = Nothing

        Public Sub New(ByVal s As isyncslice)
            assert(Not s Is Nothing)
            Me.s = s
        End Sub

        Public Function syncslice() As isyncslice
            Return s
        End Function

        Public Shared Function sync_to_async(Of T)(ByVal result As pointer(Of T),
                                                   ByVal d As _do(Of T, Boolean)) As eventComb
            assert(Not d Is Nothing)
            Return New eventComb(Function() As Boolean
                                     Dim r As T = Nothing
                                     Return d(r) AndAlso
                                            eva(result, r) AndAlso
                                            goto_end()
                                 End Function)
        End Function

        Public Function capacity(ByVal result As pointer(Of Int64)) As eventComb Implements iasyncslice.capacity
            Return sync_to_async(result, Function(ByRef r) s.capacity(r))
        End Function

        Public Function delete(ByVal key As String,
                               ByVal result As pointer(Of Boolean)) As eventComb Implements iasyncslice.delete
            Return sync_to_async(result, Function(ByRef r) s.delete(key, r))
        End Function

        Public Function empty(ByVal result As pointer(Of Boolean)) As eventComb Implements iasyncslice.empty
            Return sync_to_async(result, Function(ByRef r) s.empty(r))
        End Function

        Public Function full(ByVal result As pointer(Of Boolean)) As eventComb Implements iasyncslice.full
            Return sync_to_async(result, Function(ByRef r) s.full(r))
        End Function

        Public Function heartbeat(ByVal result As pointer(Of Boolean)) As eventComb Implements iasyncslice.heartbeat
            Return sync_to_async(result, Function(ByRef r) s.heartbeat(r))
        End Function

        Public Function keycount(ByVal result As pointer(Of Int64)) As eventComb Implements iasyncslice.keycount
            Return sync_to_async(result, Function(ByRef r) s.keycount(r))
        End Function

        Public Function list(ByVal result As pointer(Of vector(Of String))) As eventComb Implements iasyncslice.list
            Return sync_to_async(result, Function(ByRef r) s.list(r))
        End Function

        Public Function modify(ByVal key As String,
                               ByVal buff As bytes,
                               ByVal result As pointer(Of Boolean)) As eventComb Implements iasyncslice.modify
            Return sync_to_async(result, Function(ByRef r) s.modify(key, buff, r))
        End Function

        Public Function read(ByVal key As String,
                             ByVal result As pointer(Of bytes)) As eventComb Implements iasyncslice.read
            Return sync_to_async(result, Function(ByRef r) s.read(key, r))
        End Function

        Public Function retire(ByVal result As pointer(Of Boolean)) As eventComb Implements iasyncslice.retire
            Return sync_to_async(result, Function(ByRef r) s.retire(r))
        End Function

        Public Function seek(ByVal key As String,
                             ByVal result As pointer(Of Boolean)) As eventComb Implements iasyncslice.seek
            Return sync_to_async(result, Function(ByRef r) s.seek(key, r))
        End Function

        Public Function sizeof(ByVal key As String,
                               ByVal result As pointer(Of Int64)) As eventComb Implements iasyncslice.sizeof
            Return sync_to_async(result, Function(ByRef r) s.sizeof(key, r))
        End Function

        Public Sub [stop]() Implements iasyncslice.stop
            s.stop()
        End Sub

        Public Function valuesize(ByVal result As pointer(Of Int64)) As eventComb Implements iasyncslice.valuesize
            Return sync_to_async(result, Function(ByRef r) s.valuesize(r))
        End Function

        Public Function write(ByVal key As String,
                              ByVal buff As bytes,
                              ByVal result As pointer(Of Boolean)) As eventComb Implements iasyncslice.write
            Return sync_to_async(result, Function(ByRef r) s.write(key, buff, r))
        End Function
    End Class
End Namespace
